<!--
 * @Description: 用户寝室推广任务委派批次
 * @Author: liyujie
 * @Date: 2021/3/16
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/3/16
-->
<template>
    <div class="wrapper dorm-task-series-appoint">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    委派批次
                    <span class="header-tips">选择需要的批次进行委派</span>
                </div>
            </div>
            <!-- /header -->
            <div class="series-list">
                <div class="filter-view">
                    <div class="header-new-box">
                        <p class="header-new-box-title">
                            公司名称
                        </p>
                        <el-input
                            v-model="filterData.company_name"
                            placeholder="请输入公司名称"
                            size="medium"
                            clearable
                            style="width: 300px"
                            @keydown.enter.native="getTableData()"
                        />
                        <!--<el-select
                            v-model="filterData.company_name"
                            filterable
                            remote
                            clearable
                            size="medium"
                            reserve-keyword
                            placeholder="请输入公司名称"
                            :remote-method="remoteMethod"
                            @change="getTableData()"
                        >
                            <el-option
                                v-for="item in suggestList"
                                :key="item.hire_no"
                                :label="item.company_name"
                                :value="item.company_name"
                            />
                        </el-select>-->
                    </div>
                </div>
                <!-- table -->
                <el-table
                    :data="tableData"
                    height="calc(100% - 60px)"
                    style="width: 100%;"
                    class="data-table"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                    />
                    <el-table-column
                        prop="title"
                        label="批次组名称"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <!--<el-table-column
                        prop="series_title"
                        label="产品名称"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="series_index"
                        label="批次"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />-->
                    <el-table-column
                        label="寝室性别"
                        show-overflow-tooltip
                        align="center"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.sex === 1 ? '男' : scope.row.sex === 2 ? '女' : '不限' }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="count"
                        label="目标数量"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        prop="count_last"
                        label="剩余数量"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        label="委派状态"
                        width="120"
                    >
                        <template slot-scope="scope">
                            <div class="status-wrapper">
                                <div :class="`status-dot ${scope.row.status === 1 ? 'normal' : 'success'}`"/>
                                <div
                                    class="status-name"
                                    v-if="scope.row.appoint === 1"
                                >
                                    已委派
                                </div>
                                <div
                                    class="status-name"
                                    v-if="scope.row.appoint === 0"
                                >
                                    未委派
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                :disabled="scope.row.appoint === 1"
                                @click="handleSelect(scope)"
                            >
                                选择
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="select-series">
                <div class="block-title">已选批次</div>
                <el-table
                    :data="selectedSeries"
                    height="calc(100% - 42px)"
                    style="width: 100%;"
                    class="data-table"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                    />
                    <el-table-column
                        prop="title"
                        label="批次组名称"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <!--<el-table-column
                        prop="series_title"
                        label="产品名称"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="series_index"
                        label="批次"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />-->
                    <el-table-column
                        label="寝室性别"
                        show-overflow-tooltip
                        align="center"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.sex === 1 ? '男' : scope.row.sex === 2 ? '女' : '不限' }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="count"
                        label="目标数量"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        prop="count_last"
                        label="剩余数量"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        label="委派状态"
                        width="120"
                    >
                        <template slot-scope="scope">
                            <div class="status-wrapper">
                                <div :class="`status-dot ${scope.row.status === 1 ? 'normal' : 'success'}`"/>
                                <div
                                    class="status-name"
                                    v-if="scope.row.appoint === 1"
                                >
                                    已委派
                                </div>
                                <div
                                    class="status-name"
                                    v-if="scope.row.appoint === 0"
                                >
                                    未委派
                                </div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                @click="handleCancelSelect(scope)"
                            >
                                取消
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <!-- footer -->
            <div class="footer">
                <el-button
                    size="medium"
                    style="width: 120px;margin-right: 32px"
                    @click="handleRoute('dormTaskDetail', {no: student_no, from_no})"
                >
                    取消委派
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px"
                    @click="handleSubmit"
                >
                    确定委派
                </el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { deepClone } from "@/assets/js/utils";
import { back } from '@/mixin/back';

export default {
    name: 'DormTaskSeriesAppoint',
    mixins: [back],
    data() {
        return {
            student_no: '',
            from_no: '',
            filterData: {
                company_name: ''
            },
            tableData: [],
            selectedSeries: [],
            suggestList: [],
            authResult: {}
        };
    },
    mounted() {
        let query = this.$route.query;

        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'},
            {title: '精英任务领取列表',path: '/student/elite-task-receive-list?active=dorm'},
            {title: '任务详情',path: `/student/dorm-task-detail?no=${query.no}&from_no=${query.from_no}`},
            {title: '委派批次'}]);
        this.authResult = this.$route.meta.authResult;
        this.student_no = query.no;
        this.from_no = query.from_no;
        this.getSchoolTree();
        this.getSuggest();
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取列表数据
         */
        getTableData() {
            this.tableLoading = true;
            let params = { ...this.filterData };

            this.$post('/student/room_series_student_appoint%5Cget_series_list_for_appoint', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    // this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 提交委派批次
         */
        handleSubmit() {
            if(!this.selectedSeries.length) {
                this.$message.warning('请先选择批次');
                return;
            }

            let series_nos = deepClone(this.selectedSeries).map(item => {
                return item.series_no;
            }).join();
            let params = {
                student_no: this.student_no,
                series_nos,
                from_type: 1,
                from_no: this.from_no
            };

            params.student_no = this.student_no;
            this.$post('/student/room_series_student_appoint%5Cappoint', params, res => {
                if (res.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '委派批次成功',
                        type: 'success'
                    });
                    this.handleRoute('dormTaskDetail', {no: this.student_no, from_no: this.from_no});
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },
        /**
         * 选择
         * @param row
         * @param $index
         */
        handleSelect({row, $index}) {
            this.selectedSeries.push(deepClone(row));
            this.tableData.splice($index, 1);
        },
        /**
         * 取消选择
         * @param row
         * @param $index
         */
        handleCancelSelect({row, $index}) {
            // this.tableData.push(deepClone(row));
            this.selectedSeries.splice($index, 1);
            let params = { ...this.filterData };

            this.$post('/student/room_series_student_appoint%5Cget_series_list_for_appoint', params, res => {
                if (res.code === 1) {
                    let tableData = res.data.list;
                    let series_nos = this.selectedSeries.map(item => {
                        return item.series_no;
                    });

                    this.tableData = tableData.filter(item => {
                        return !series_nos.includes(item.series_no);
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * @description 搜索公司名称
         */
        remoteMethod (query) {
            if (query !== '') {
                setTimeout(() => {
                    this.getSuggest(query);
                }, 200);
            }
        },

        /**
         * @description 获取商家列表
         */
        getSuggest (keyword) {
            this.$post('/student/room_series%5Cget_company_list', {
                keyword: keyword
            }, resp => {
                if (resp.code == 1) {
                    this.suggestList = resp.data.list;
                }
            });
        },
    }
};
</script>

<style scoped lang="less">
.wrapper {
    width: 100%;
    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;
        padding-bottom: 76px;
        .header {
            height: 66px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;
            padding: 24px;

            .header-title {
                color: #333333;
                font-size: 20px;
                font-weight: 500;
            }
            .header-tips {
                font-size: 14px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                margin-left: 12px;
            }
        }
        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
            .status-wrapper {
                display: flex;
                align-items: center;

                .status-dot {
                    width: 6px;
                    height: 6px;
                    border-radius: 50%;
                    margin-right: 5px;

                    &.success {
                        background: #4CD964;
                    }

                    &.danger {
                        background: #FF3030;
                    }

                    &.warning {
                        background: #FFA72D;
                    }

                    &.normal {
                        background: #C2C2C2;
                    }
                }
            }
        }

        .series-list {
            padding: 24px 48px 0;
            flex: 1;
            overflow: hidden;
        }
        .select-series {
            flex: 1;
            overflow: hidden;
            margin: 24px;
            padding: 24px;
            background: #FBFBFB;
            border-radius: 2px;
            border: 1px solid #EEEEEE;
            .data-table {
                background: #FBFBFB;
            }
            .block-title {
                font-size: 18px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 24px;
            }
        }

        .footer {
            width: 100%;
            height: 76px;
            position: absolute;
            bottom: 0;
            left: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            border-top: 1px solid #eeeeee;
        }
    }
}
</style>
<style lang="less">
.wrapper.dorm-task-series-appoint {
    .el-table {
        th {
            background: #f2f2f2!important;
        }
    }
    .select-series {
        td {
            background: #FBFBFB;
        }
    }
}
</style>
